let app = getApp()
const util = require('../../../utils/util.js')

Page({

    /**
     * 页面的初始数据
     */
    data: {
        btnDisable: false,
        spinShow: false,
        cargoId: "",
        cargoNo: "",
        cargoName: "",
        purchasePrice: "",
        wholesalePrice: "",
        remark: "",
        fileList: [],
        picUrl: null,
        //日期组件
        show: false,
        //入库单选择商品界面
        chooseNotExistsAdd: false,

        //选中的颜色
        checkedColors: [],
        checkedColorNames: "",
        //选中的尺码
        checkedSizes: [],
        checkedSizeNames: ""
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        //判断是否在销售单选择客户界面
        let chooseNotExistsAdd = options.chooseNotExistsAdd
        if (chooseNotExistsAdd) {
            this.setData({
                chooseNotExistsAdd: chooseNotExistsAdd
            })
        }
        let cargoId = options.cargoId
        if (cargoId) {
            this.setData({
                cargoId: cargoId
            })
            this.queryData()
        }
    },
    onShow: function () {
        console.log(this.data.checkedColors)
        let checkedColors = this.data.checkedColors
        if (checkedColors && checkedColors.length > 0) {
            this.setData({
                // checkedColorNames: checkedColors.filter(t => t.checked).map(t => t.name).join(',')
                checkedColorNames: checkedColors.join(',')
            })
        }
        let checkedSizes = this.data.checkedSizes
        if (checkedSizes && checkedSizes.length > 0) {
            this.setData({
                checkedSizeNames: checkedSizes.join(',')
            })
        }
    },

    queryData: function () {
        let _this = this
        app.formGet('/app/cargo/getByCargoId?cargoId=' + this.data.cargoId, null).then(res => {
            if (res.code === 0) {
                console.log("res.data: %o", res.data)
                if (res.data != null) {
                    let imageObj = {}
                    let tempFileList = []
                    let picUrl = res.data.picUrl
                    imageObj.url = picUrl
                    imageObj.isImage = true
                    tempFileList.push(imageObj)
                    _this.setData({
                        fileList: tempFileList,
                        picUrl: picUrl,
                        id: res.data.id,
                        cargoNo: res.data.cargoNo,
                        cargoName: res.data.cargoName,
                        remark: res.data.remark,
                        purchasePrice: res.data.purchasePrice,
                        wholesalePrice: res.data.wholesalePrice,
                        checkedColors: res.data.colorList,
                        checkedSizes: res.data.sizeList,
                        checkedColorNames: res.data.colorList.join(','),
                        checkedSizeNames: res.data.sizeList.join(','),

                    })
                }
            } else {
                console.log("error")
                app.message(res.message, 'error')
            }
        })
    },
    handleEditCargo() {
        if (this.data.cargoNo == '') {
            app.toast("请输入款号", "fail")
            return false;
        }
        if (this.data.cargoName == '') {
            app.toast("请输入款名称", "fail")
            return false;
        }

        let _this = this
        let cargoData = {}
        cargoData.id = this.data.cargoId
        cargoData.picUrl = this.data.picUrl
        cargoData.cargoName = this.data.cargoName
        cargoData.cargoNo = this.data.cargoNo
        cargoData.purchasePrice = this.data.purchasePrice
        cargoData.wholesalePrice = this.data.wholesalePrice
        cargoData.remark = this.data.remark
        cargoData.colors = this.data.checkedColorNames
        cargoData.sizes = this.data.checkedSizeNames
        _this.setData({
            btnDisable: true
        })
        let url = "/app/cargo/edit"
        app.formPost(url, cargoData).then(res => {
            _this.setData({
                spinShow: false
            });
            if (res.code === 0) {
                app.message(res.message, 'success')
                //选择商品没有新增，则新增后直接返回到原引用页面
                if (_this.data.chooseNotExistsAdd) {
                    let pages = getCurrentPages();
                    let prevPage = pages[pages.length - 3];
                    prevPage.setData({
                        chooseCargoId: res.data
                    })
                    let searchCargo = prevPage.searchCargo
                    if (searchCargo) {
                        searchCargo()
                    }
                    wx.navigateBack({
                        delta: 2,
                    })
                    _this.setData({
                        btnDisable: false
                    })
                } else {
                    setTimeout(function () {
                        wx.navigateBack({
                            delta: 1
                        })
                    }, 2000)
                    _this.setData({
                        btnDisable: false
                    })
                }
            } else {
                app.message(res.message, 'error')
            }
        }).catch(e => {
            _this.setData({
                spinShow: false,
                btnDisable: false
            });
            app.message(e, 'error')
        })
    },
    picAfterRead(event) {
        const {
            file
        } = event.detail;
        // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
        let _this = this
        wx.uploadFile({
            url: app.globalData.baseAPI + '/app/upload/pic', // 仅为示例，非真实的接口地址
            filePath: file.url,
            name: 'file',
            formData: {},
            header: {
                'Authorization': wx.getStorageSync('token')
            },
            success(res) {
                // 上传完成需要更新 fileList
                let result = JSON.parse(res.data)
                if (result.code === 0) {
                    let imageObj = {}
                    let tempFileList = []
                    imageObj.url = result.data
                    imageObj.isImage = true
                    tempFileList.push(imageObj)
                    _this.setData({
                        fileList: tempFileList,
                        picUrl: result.data
                    })
                }
            },
            fail(res) {
                console.log("fail")
                console.log(res)
            }
        });
    },
    deleteFile: function (e) {
        let index = e.detail.index
        let fileList = this.data.fileList
        fileList.splice(index, 1)
        this.setData({
            fileList: fileList
        })
    },
    colorCatchTag: function () {
        wx.navigateTo({
            url: "/pages/cargo/choose/color/chooseColor?checkedColorList=" + JSON.stringify(this.data.checkedColors)
        })
    },
    sizeCatchTag: function () {
        wx.navigateTo({
            url: "/pages/cargo/choose/size/chooseSize?checkedSizeList=" + JSON.stringify(this.data.checkedSizes)
        })
    }
})